<%--
  Created by IntelliJ IDEA.
  User: xiaox
  Date: 2024/12/28
  Time: 15:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
    <style>
        /* 整体页面布局，使内容垂直水平居中 */
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: #b9def0;
        }

        /* 整体表单容器的样式 */
        .form-div {
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 300px;
            /*margin: 0 auto;*/
        }

        /* 注册内容区域的样式 */
        .reg-content {
            margin-bottom: 20px;
            text-align: center;
        }

        /* 标题样式 */
        .reg-content h1 {
            color: #333;
        }

        /* 链接样式 */
        .reg-content a {
            color: #007BFF;
            text-decoration: none;
            margin-left: 5px;
        }

        /* 表单标签样式 */
        form label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        /* 输入框样式 */
        form input[type="text"],
        form input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
            margin-bottom: 10px;
        }

        /* 错误消息样式 */
        .err_msg {
            color: red;
            font-size: 12px;
        }

        /* 使登录按钮与用户名输入框等宽 */
        form input[type="submit"] {
            width: 100%;
            background-color: #007BFF;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

    </style>
</head>
<body>
<div class="form-div">
    <div class="reg-content" style="text-align: center">
        <h1>欢迎登录</h1>
        <a href="register.jsp">没有账号？点击注册</a>
    </div>
    <form action="login" method="post" id="reg-form">
        <label for="username">用户名：</label>
        <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        <br>
        <span id="username_err" class="err_msg" style="display:none">用户名格式有误</span>

        <label for="password">密码：</label>
        <input type="password" id="password" name="password" placeholder="请输入密码" required>
        <br>
        <span id="password_err" class="err_msg" style="display: none">密码格式有误</span>
        <a href="forgot-password.jsp" style = "text-align: right;text-decoration: none;color: #007BFF; display: block; margin: 0 auto;">忘记密码？</a>
        <input type="submit" value="登录">

    </form>
</div>
</body>
<script>
    //验证用户名
    var usernameInput = document.getElementById("username");
    usernameInput.onblur = checkUser;
    function checkUser(){
        var username = usernameInput.value.trim();
        //判断用户名是否符合规则：长度6~12 单词字符组成
        //正则表达式
        var reg = /^\w{1,12}$/;
        var flag= reg.test(username);
        if(flag){
            //符合规则
            document.getElementById("username_err").style.display = "none";
        }else{
            //不符合规则，将span标签显示出来，给出用户提示。
            document.getElementById("username_err").style.display = "";
        }
        return flag;
    }

    //验证密码
    var passwordInput = document.getElementById("password");
    passwordInput.onblur = checkPassword;
    function checkPassword(){
        var password = passwordInput.value.trim();
        var reg = /^\w{6,12}$/;
        var flag= reg.test(password);
        if(flag){
            document.getElementById("password_err").style.display = "none";
        }else{
            document.getElementById("password_err").style.display = "";
        }
        return flag;
    }

    //验证手机号
    const telInput = document.getElementById("phone");
    telInput.onblur = checkTel;
    function checkTel(){
        const phone = telInput.value.trim();
        const reg = /^[1]\d{10}$/;
        const flag = reg.test(phone);
        if(flag){
            document.getElementById("tel_err").style.display = "none";
        }else{
            document.getElementById("tel_err").style.display = "";
        }
        return flag;
    }

    //验证表单
    //获取表单对象
    var regForm = document.getElementById("reg-form");
    //绑定onsubmit事件
    regForm.onsubmit = function(){
        //挨个判断每一个表单项是否都符合要求，如果有一个不合符，则返回false
        var flag = checkUser()&&checkPassword()&&checkTel();
        return flag;
    }



</script>
</html>
